#include "rclcpp/rclcpp.hpp"
#include <vector>
class ParamServer:public rclcpp::Node
{
public:
    ParamServer():Node("server_param_node",
    rclcpp::NodeOptions().allow_undeclared_parameters(true))
    //(const std::string &node_name, 
    //const std::string &namespace_, 
    //const rclcpp::NodeOptions &options = rclcpp::NodeOptions())
    {
        RCLCPP_INFO(this->get_logger(),"参数客户端创建了");
    
    }   
    void declare_param()
    {
        this->declare_parameter("car_name","tiger");
        this->declare_parameter("height",1.55);
        this->declare_parameter("wheels",5);

        //用set_parameter设置参数的前提是Node设置好了参数
        this->set_parameter(rclcpp::Parameter("width",2.1));
    }
    void get_param()
    {
        rclcpp::Parameter param= this->get_parameter("height");
        RCLCPP_INFO(this->get_logger(),"key=%s",param.get_name().c_str());
        RCLCPP_INFO(this->get_logger(),"value=%.2f",param.as_double());
        //将参数值以字符串形式输出
        RCLCPP_INFO(this->get_logger(),"value=%s",param.value_to_string().c_str());
        
        
        std::vector<rclcpp::Parameter>params=this->get_parameters({"car_name","height","wheels"});
        for(rclcpp::Parameter param:params)
        {
          RCLCPP_INFO(this->get_logger(),"%s:%s",param.get_name().c_str(),
          param.value_to_string().c_str());
        }
        RCLCPP_INFO(this->get_logger(),"参数是否包含car_name?%d",this->has_parameter("car_name"));
        RCLCPP_INFO(this->get_logger(),"参数是否包含car?%d",this->has_parameter("car"));
        
    }
    void update_param()
    {
        //参数是一个Parameter类的一个对象或匿名对象
        rclcpp::Parameter p("car_name","BMW");
        this->set_parameter(p);
        RCLCPP_INFO(this->get_logger(),"car_name=%s",
        this->get_parameter("car_name").as_string().c_str());
        this->set_parameter(rclcpp::Parameter("height",1.75));
        RCLCPP_INFO(this->get_logger(),"height=%.2f",
        this->get_parameter("height").as_double());
    }
    void del_param()
    {
        //只能删除set的参数，不能删除declare的参数 和python不一样
        RCLCPP_INFO(this->get_logger(),"删除前包含width吗%d",
        this->has_parameter("width"));
        this->undeclare_parameter("width");
        RCLCPP_INFO(this->get_logger(),"删除后包含width吗%d",
        this->has_parameter("width"));
    }
};
int main(int argc,const char*const argv[])
{
    rclcpp::init(argc,argv);
    auto node=std::make_shared<ParamServer>();
    node->declare_param();
    node->get_param();
    node->update_param();
    node->del_param();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}